<?php

Loader::using("Models::Entities::Response");

/**
 * Description of Autenticarse
 *
 * @author JeyDotC
 * 
 * @export Modules
 */
class Autenticarse extends Module {

    public function index() {
        if ($this->currentUser->isAnonimous()) {
            $return = $this->request->get("return");
            $this->assign("action", $this->route->action("login", array("return" => $return,)));

            $this->display("index");
        } else {
            $this->assign("logout", $this->route->action("logout"));
            $this->assign("user", $this->currentUser);
            $this->display("noPermissions");
        }
    }

    public function login() {
        $user = $this->request->post("user");
        $pass = $this->request->post("pass");

        $succeed = $this->components->Authentication->login($user, $pass);

        if ($succeed) {
            $url = $this->request->get("return", null);
            $url = $url ? base64_decode($url) : $this->route->appAction("Default", "FrontPage");
            $this->route->redirect($url);
        } else {
            $this->addMessage(new Message("Your username or password is incorrect", "Incorrect username or password", Message::ERROR));
            $this->index();
        }
    }

    public function logout() {
        $this->request->getSession()->close();
        $this->route->redirect($this->route->appAction("Default", "FrontPage"));
    }

}

?>
